﻿using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using DemoLib.Service.Client;
using DemoLib.Service.Client.Clients;
using DemoLib.Shared.Contracts;
using Microsoft.Practices.Unity;
using Mvc4Demo.Core;

namespace Mvc4Demo
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //BundleTable.EnableOptimizations = true;

            AuthConfig.RegisterAuth();
            SetDependencyResolver();

            ViewEngines.Engines.Add(new MobileViewEngine());
            ViewEngines.Engines.Add(new PdfViewEngineProvider());
        }

        private static void SetDependencyResolver()
        {
            var container = BuildUnityContainer();

            var dependencyResolver = new UnityDependencyResolver(container);
            DependencyResolver.SetResolver(dependencyResolver);

            GlobalConfiguration.Configuration.DependencyResolver = new WebpiUnityDependencyResolver(dependencyResolver);
        }

        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();

            container.RegisterType<IControllerActivator, CustomControllerActivator>();

            // e.g. container.RegisterType<ITestService, TestService>();

            container.RegisterType<IUserService, UserServiceClient>();
            container.RegisterType<IEmailService, EmailServiceClient>();
            container.RegisterType<IAuthoService, AuthoServiceClient>();

            return container;
        }
    }
}